<template>
  <el-col>
    <el-dialog title="增加" :visible.sync="addDialogVisible" :before-close="handleCloseAddDialog">
      <el-form ref="sectionInfo" :model="sectionInfo" label-width="100px">
        <el-form-item label="医院名" prop="hospitalId" v-if="hospitalId === 'null'">
          <el-select v-model="sectionInfo.hospitalId" placeholder="">
            <el-option v-for="(item, index) in hospitalList" :key="index" :value="item.hospitalId"
              :label="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="科室名" prop="name">
          <el-input v-model="sectionInfo.name" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCloseAddDialog">取 消</el-button>
        <el-button type="primary" @click="handleConfirmAddDialog">确 定</el-button>
      </span>
    </el-dialog>

    <el-dialog title="更新" :visible.sync="updateDialogVisible" :before-close="handleCloseUpdateDialog">
      <el-form ref="sectionInfo" :model="sectionInfo" label-width="100px">
        <el-form-item label="医院名" prop="hospitalId" v-if="hospitalId === 'null'">
          <el-select v-model="sectionInfo.hospitalId" placeholder="">
            <el-option v-for="(item, index) in hospitalList" :key="index" :value="item.hospitalId"
              :label="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="科室名" prop="name">
          <el-input v-model="sectionInfo.name" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCloseUpdateDialog">取 消</el-button>
        <el-button type="primary" @click="handleConfirmUpdateDialog">确 定</el-button>
      </span>
    </el-dialog>

    <el-row>
      <el-col :span="2">
        <el-button type="primary" @click="addDialogVisible = true">增加</el-button>
      </el-col>
      <el-col :span="2">
        <el-button :disabled="multipleSelection.length > 1" type="warning" @click="showUpdateDialogVisible">更新
        </el-button>
      </el-col>
      <el-col :span="2">
        <el-button type="danger" @click="handleDeleteSelectIds">删除</el-button>
      </el-col>
      <el-col :span="5">
        <el-input v-model="query.name" type="text" placeholder="科室名" />
      </el-col>
      <el-col :span="5" v-if="hospitalId === 'null'">
        <el-select v-model="query.hospitalId" placeholder="医院">
          <el-option v-for="(item, index) in hospitalList" :key="index" :value="item.hospitalId"
            :label="item.name"></el-option>
        </el-select>
      </el-col>
      <el-col :span="2">
        <el-button @click="queryClear"> 清除 </el-button>
      </el-col>
      <el-col :span="2">
        <el-button @click="doSelectAll"> 搜索 </el-button>
      </el-col>
    </el-row>

    <el-table ref="multipleTable" :data="sectionList" style="width: 100%" row-key="sectionId" lazy
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" />
      <el-table-column prop="sectionId" label="科室 ID" />
      <el-table-column prop="hospitalName" label="医院名" />
      <el-table-column prop="name" label="科室名" />
    </el-table>
    <el-pagination background layout="prev, pager, next" :total="total" prev-click="" next-click="" />
  </el-col>
</template>

<script>
export default {
  name: "MSection",
  data() {
    return {
      addDialogVisible: false,
      updateDialogVisible: false,

      sectionId: 0,

      sectionList: [],
      hospitalList: [],

      page: 0,
      pageSize: 10,
      total: 0,

      multipleSelection: [],

      sectionInfo: {
        sectionId: "",
        hospitalId: "",
        name: "",
      },

      query: {
        name: "",
        hospitalId: "",
      },
    };
  },
  created() {
    this.hospitalId = localStorage.getItem('hospitalId') === "null" ? 'null' : parseInt(localStorage.getItem('hospitalId'))
    if (this.hospitalId === "null") {
      this.doSelectAllHospital();
    } else {
      this.query.hospitalId = this.hospitalId
    }
    this.doSelectAll();
  },
  methods: {
    currentChange(index) {
      this.page = index - 1;
    },
    showUpdateDialogVisible() {
      if (this.multipleSelection.length === 0) {
        this.$message.error("请选择");
        return;
      }
      this.sectionInfo = this.multipleSelection[0];
      this.updateDialogVisible = true;
    },

    showMenuDialogVisible(sectionId) {
      this.sectionInfo = sectionId;
      this.menuDialogVisible = true;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    handleCloseAddDialog() {
      this.addDialogVisible = false;
      this.refresh();
    },

    handleConfirmAddDialog() {
      this.$refs.sectionInfo.validate((valid) => {
        if (valid) {
          this.addDialogVisible = false;
          this.$store.dispatch("section/add", this.sectionInfo).then(() => { });
        }
      });
      this.refresh();
    },
    handleCloseUpdateDialog() {
      this.updateDialogVisible = false;
      this.refresh();
    },
    handleConfirmUpdateDialog() {
      this.$refs.sectionInfo.validate((valid) => {
        if (valid) {
          this.updateDialogVisible = false;
          this.$store
            .dispatch("section/update", this.sectionInfo)
            .then(() => { });
        }
      });
      this.refresh();
    },
    handleCloseMenuDialog() {
      this.menuDialogVisible = false;
    },
    handleDeleteSelectIds() {
      const ids = [];
      for (const obj of this.multipleSelection) {
        ids.push(obj.sectionId);
      }
      this.$store.dispatch("section/delete", ids).then(() => { });
      this.doSelectAll();
    },
    doSelectAll() {
      this.$store
        .dispatch("section/selectAll", {
          obj: {
            name: this.query.name,
            hospitalId: this.query.hospitalId != "" ? this.query.hospitalId : 0,
          },
          page: this.page,
          pageSize: this.pageSize,
        })
        .then((res) => {
          this.sectionList = res.list;
          this.total = res.total;
        });
    },
    doSelectAllHospital() {
      this.$store.dispatch("hospital/allId", {}).then((res) => {
        this.hospitalList = res;
      });
    },

    queryClear() {
      this.query = {
        name: "",
        hospitalId: this.hospitalId === 'null' ? "" : this.hospitalId,
      }
      this.doSelectAll()
    },

    prevClick() {
      this.page -= 1;
      this.doSelectAll();
    },
    nextClick() {
      this.page += 1;
      this.doSelectAll();
    },
    refresh() {
      this.sectionInfo = {
        hospitalId: this.hospitalId === 'null' ? "" : this.hospitalId,
        name: "",
      };
      this.doSelectAll();
      this.$refs.multipleTable.clearSelection();
    },
  }
}
</script>

<style scoped>

</style>